package org.jjj.org.basic.service.impl;

import org.jjj.org.basic.mapper.IBaseMapper;
import org.jjj.org.basic.query.BaseQuery;
import org.jjj.org.basic.service.IBaseService;
import org.jjj.org.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private IBaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.insert(t);
    }


    @Transactional
    @Override
    public void deleteById(Serializable id) {
        baseMapper.deleteById(id);
//        System.out.println(1/0);

    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);

    }

    @Override
    public T selectById(Serializable id) {

        return baseMapper.selectById(id);
    }

    @Override
    public List<T> selectAll() {

        return baseMapper.selectAll();
    }

    /**
     * 分页查询和高级查询的数据封装service的在一个方法中
     * @param query
     * @return
     */
    @Override
    public PageList<T> queryPage(BaseQuery query) {
        //先根据条件查询总条数
        long total = baseMapper.selectCount(query);
        if (total < 1){
            return new PageList<T>();
        }
        List<T> rows = baseMapper.selectDatas(query);
        return new PageList<T>(total,rows);
    }
    //批量删除
    @Override
    @Transactional
    public void batchRemove(List<Long> ids) {

        baseMapper.batchRemove(ids);
    }
}
